/******************************************************************************
 * * call_log_proc.c - process user key event of alarm module
 *
 * *(C) Copyright 2019 Asr International Ltd.
 * * All Rights Reserved
 * ******************************************************************************/

#include "alarm_global.h"

/**
 * process button Alarm -> Set Alarm
 */
VOID Alarm_Proc_Btn_Set_Alarm(lv_obj_t *Btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED != e) {
        return;
    }

    Display_Alarm_Set();
}

/**
 * process button Alarm -> Alarm Tone
 */
VOID Alarm_Proc_Btn_Alarm_Tone(lv_obj_t *Btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED != e) {
        return;
    }

    Display_Alarm_Tone();
}

/**
 * process key event of Alarm
 */
UINT8 Alarm_Proc_Key_Alarm(lv_obj_t *obj, UI_KEY_STATUS key_sta, UI_KEY_VALUE key_val)
{
    UINT8 Ret = 0;                        /* 0: call default signal func, 1: otherwise */
    UI_Normal_Container_List_Desc_t *Desc = (UI_Normal_Container_List_Desc_t *)g_Alarm_Mng.CurrDesc;

    if (KEY_RELEASED == key_sta)
    {
        switch (key_val)
        {
        case KEY_SOFTRIGHT:
            Nav_Back(ACT_ID_ANY);
            Ret = 1;
            break;

        case KEY_END:
            Nav_Home(ACT_ID_ANY);
            Ret = 1;
            break;

        case KEY_OK:
            break;

        case KEY_UP:
        case KEY_LEFT:
            if (Desc->ContList.Index > 0)
            {
                Desc->ContList.Index--;
            }
            else
            {
                Desc->ContList.Index = Desc->ContList.ListContainerCnt - 1;
            }
            break;

        case KEY_DOWN:
        case KEY_RIGHT:
            if (Desc->ContList.Index + 1 < Desc->ContList.ListContainerCnt)
            {
                Desc->ContList.Index++;
            }
            else
            {
                Desc->ContList.Index = 0;
            }
            break;

        default:
            break;
        }
    }

    return(Ret);
}

/**
 * process button Set Alarm -> Alarm1
 */
VOID Alarm_Proc_Btn_Alarm1(lv_obj_t *Btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED != e) {
        return;
    }

    g_Alarm_Mng.CurrAlarmId = 0;

    if (TRUE == g_Alarm_Mng.Alarm.AlarmInfo[0].OnOff)    // On
    {
        Display_Alarm_Edit();
    }
    else    // Off
    {
        Display_Alarm_Set_Time(TRUE);
    }
}

/**
 * process button Set Alarm -> Alarm2
 */
VOID Alarm_Proc_Btn_Alarm2(lv_obj_t *Btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED != e) {
        return;
    }

    g_Alarm_Mng.CurrAlarmId = 1;

    if (TRUE == g_Alarm_Mng.Alarm.AlarmInfo[1].OnOff)    // On
    {
        Display_Alarm_Edit();
    }
    else    // Off
    {
        Display_Alarm_Set_Time(TRUE);
    }
}

/**
 * process key event of Alarm Set
 */
UINT8 Alarm_Proc_Key_Alarm_Set(lv_obj_t *obj, UI_KEY_STATUS key_sta, UI_KEY_VALUE key_val)
{
    UINT8 Ret = 0;                        /* 0: call default signal func, 1: otherwise */
    UI_Normal_Container_List_Desc_t *Desc = (UI_Normal_Container_List_Desc_t *)g_Alarm_Mng.CurrDesc;

    if (KEY_RELEASED == key_sta)
    {
        switch (key_val)
        {
        case KEY_SOFTRIGHT:
            Nav_Back(ACT_ID_ANY);
            Ret = 1;
            break;

        case KEY_END:
            Nav_Home(ACT_ID_ANY);
            Ret = 1;
            break;

        case KEY_OK:
            break;

        case KEY_UP:
        case KEY_LEFT:
            if (Desc->ContList.Index > 0)
            {
                Desc->ContList.Index--;
            }
            else
            {
                Desc->ContList.Index = Desc->ContList.ListContainerCnt - 1;
            }
            break;

        case KEY_DOWN:
        case KEY_RIGHT:
            if (Desc->ContList.Index + 1 < Desc->ContList.ListContainerCnt)
            {
                Desc->ContList.Index++;
            }
            else
            {
                Desc->ContList.Index = 0;
            }
            break;

        default:
            break;
        }
    }

    return(Ret);
}

/**
 * process key event of Alarm Set Time
 */
UINT8 Alarm_Proc_Key_Alarm_Set_Time(lv_obj_t *obj, UI_KEY_STATUS key_sta, UI_KEY_VALUE key_val)
{
    UINT8                 Ret     = 0; /* 0: call default signal func, 1: otherwise */
    UINT8                 AlarmId = g_Alarm_Mng.CurrAlarmId;
    UI_Normal_Time_Desc_t *Desc   = (UI_Normal_Time_Desc_t *)g_Alarm_Mng.CurrDesc;
    INT8                  Buf[3] = {0};

    if (KEY_RELEASED == key_sta)
    {
        switch (key_val)
        {
        case KEY_SOFTRIGHT:
            Nav_Back(ACT_ID_ANY);
            Ret = 1;
            break;

        case KEY_END:
            Nav_Home(ACT_ID_ANY);
            Ret = 1;
            break;

        case KEY_OK:
            g_Alarm_Mng.Alarm.AlarmInfo[AlarmId].OnOff = TRUE;
            lv_roller_get_selected_str(Desc->ContTime.Roller1.Roller, Buf, 0);
            g_Alarm_Mng.Alarm.AlarmInfo[AlarmId].Hour = atoi(Buf);
            lv_roller_get_selected_str(Desc->ContTime.Roller2.Roller, Buf, 0);
            g_Alarm_Mng.Alarm.AlarmInfo[AlarmId].Min = atoi(Buf);
            g_Alarm_Mng.Alarm.AlarmInfo[AlarmId].RepeatCount = 0;

            /* write to NVM */
            UI_NV_Write_Req(NV_SECTION_UI_ALARM, 0, sizeof(NV_UI_Alarm_t), (UINT8 *)&g_Alarm_Mng.Alarm);

            /* set recent alarm to rtc */
            Alarm_Set_Recent_Alarm(TRUE, FALSE);

            /* inform framework alarm on */
            Framework_Alarm_Set_Alarm_Ind(TRUE);

            /* display UI */
            Display_Alarm_Status(ACT_ID_ALARM_ON);
            Ret = 1;
            break;

        default:
            break;
        }
    }

    return(Ret);
} /* Alarm_Proc_Key_Alarm_Set_Time */

/**
 * process button Alarm1/2 -> Adjust
 */
VOID Alarm_Proc_Btn_Adjust(lv_obj_t *Btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED != e) {
        return;
    }

    Display_Alarm_Set_Time(FALSE);
}

/**
 * process button Alarm1/2 -> Off
 */
VOID Alarm_Proc_Btn_Off(lv_obj_t *Btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED != e) {
        return;
    }

    Hal_Mem_Set(&g_Alarm_Mng.Alarm.AlarmInfo[g_Alarm_Mng.CurrAlarmId], 0, sizeof(NV_UI_Alarm_Info_t));
    g_Alarm_Mng.Alarm.AlarmInfo[g_Alarm_Mng.CurrAlarmId].OnOff = FALSE;

    /* write to NVM */
    UI_NV_Write_Req(NV_SECTION_UI_ALARM, 0, sizeof(NV_UI_Alarm_t), (UINT8 *)&g_Alarm_Mng.Alarm);

    /* set recent alarm to rtc */
    Alarm_Set_Recent_Alarm(TRUE, FALSE);

    /* inform framework alarm off */
    if (  (FALSE == g_Alarm_Mng.Alarm.AlarmInfo[0].OnOff)
       && FALSE == g_Alarm_Mng.Alarm.AlarmInfo[1].OnOff)
    {
        Framework_Alarm_Set_Alarm_Ind(FALSE);
    }

    /* display UI */
    Display_Alarm_Status(ACT_ID_ALARM_OFF);
}

/**
 * process button Alarm1/2 -> Repeat
 */
VOID Alarm_Proc_Btn_Repeat(lv_obj_t *Btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED != e) {
        return;
    }

    Display_Alarm_Repeat();
}

/**
 * process key event of Alarm Edit
 */
UINT8 Alarm_Proc_Key_Alarm_Edit(lv_obj_t *obj, UI_KEY_STATUS key_sta, UI_KEY_VALUE key_val)
{
    UINT8 Ret = 0;                        /* 0: call default signal func, 1: otherwise */
    UI_Normal_List_Only_Desc_t *Desc = (UI_Normal_List_Only_Desc_t *)g_Alarm_Mng.CurrDesc;

    if (KEY_RELEASED == key_sta)
    {
        switch (key_val)
        {
        case KEY_SOFTRIGHT:
            Nav_Back(ACT_ID_ANY);
            Ret = 1;
            break;

        case KEY_END:
            Nav_Home(ACT_ID_ANY);
            Ret = 1;
            break;

        case KEY_OK:
            break;

        case KEY_UP:
        case KEY_LEFT:
            if (Desc->List.Index > 0)
            {
                Desc->List.Index--;
            }
            else
            {
                Desc->List.Index = Desc->List.ListButtonCnt - 1;
            }
            break;

        case KEY_DOWN:
        case KEY_RIGHT:
            if (Desc->List.Index + 1 < Desc->List.ListButtonCnt)
            {
                Desc->List.Index++;
            }
            else
            {
                Desc->List.Index = 0;
            }
            break;

        default:
            break;
        }
    }

    return(Ret);
}

/**
 * process button Repeat -> Alarm once
 */
VOID Alarm_Proc_Btn_Mode_Once(lv_obj_t *Btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED != e) {
        return;
    }

    g_Alarm_Mng.Alarm.AlarmInfo[g_Alarm_Mng.CurrAlarmId].RepeatBitmap = 0;

    /* write to NVM */
    UI_NV_Write_Req(NV_SECTION_UI_ALARM, 0, sizeof(NV_UI_Alarm_t), (UINT8 *)&g_Alarm_Mng.Alarm);

    /* set recent alarm to rtc */
    Alarm_Set_Recent_Alarm(TRUE, FALSE);

    Display_Alarm_Status(ACT_ID_ALARM_ONCE_SELECTED);
}

/**
 * process button Repeat -> Repeat alarm
 */
VOID Alarm_Proc_Btn_Mode_Repeat(lv_obj_t *Btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED != e) {
        return;
    }

    Display_Alarm_Repeat_Select();
}

/**
 * process key event of Alarm Repeat
 */
UINT8 Alarm_Proc_Key_Alarm_Repeat(lv_obj_t *obj, UI_KEY_STATUS key_sta, UI_KEY_VALUE key_val)
{
    UINT8 Ret = 0;                        /* 0: call default signal func, 1: otherwise */
    UI_Normal_List_Only_Desc_t *Desc = (UI_Normal_List_Only_Desc_t *)g_Alarm_Mng.CurrDesc;

    if (KEY_RELEASED == key_sta)
    {
        switch (key_val)
        {
        case KEY_SOFTRIGHT:
            Nav_Back(ACT_ID_ANY);
            Ret = 1;
            break;

        case KEY_END:
            Nav_Home(ACT_ID_ANY);
            Ret = 1;
            break;

        case KEY_OK:
            break;

        case KEY_UP:
        case KEY_LEFT:
            if (Desc->List.Index > 0)
            {
                Desc->List.Index--;
            }
            else
            {
                Desc->List.Index = Desc->List.ListButtonCnt - 1;
            }
            break;

        case KEY_DOWN:
        case KEY_RIGHT:
            if (Desc->List.Index + 1 < Desc->List.ListButtonCnt)
            {
                Desc->List.Index++;
            }
            else
            {
                Desc->List.Index = 0;
            }
            break;

        default:
            break;
        }
    }

    return(Ret);
}

/**
 * process button Repeat alarm -> Sunday/Monday/.../Saturday
 */
VOID Alarm_Proc_Btn_Repeat_Select_Wday(lv_obj_t *Btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED != e) {
        return;
    }

    UI_Normal_List_Only_Desc_t *Desc    = (UI_Normal_List_Only_Desc_t *)g_Alarm_Mng.CurrDesc;
    UINT8                      BtnIndex = 0;

    BtnIndex = lv_list_get_btn_index(Desc->List.List, Btn);

    // set img src
    lv_obj_t *Img = lv_obj_get_child_back(Btn, NULL);
    if (0 == strcmp(SYMBOL_CHECKED, lv_img_get_src(Img)))
    {
        lv_img_set_src(Img, SYMBOL_NOT_CHECKED);
        g_Alarm_Mng.RepeatBitmapTemp &= ~(1 << BtnIndex);
        Desc->List.ButtonList[Desc->List.Index].SymbolId1 = SYMBOL_NOT_CHECKED;
    }
    else
    {
        lv_img_set_src(Img, SYMBOL_CHECKED);
        g_Alarm_Mng.RepeatBitmapTemp |= (1 << BtnIndex);
        Desc->List.ButtonList[Desc->List.Index].SymbolId1 = SYMBOL_CHECKED;
    }
}

/**
 * process key event of Alarm Repeat Select
 */
UINT8 Alarm_Proc_Key_Alarm_Repeat_Select(lv_obj_t *obj, UI_KEY_STATUS key_sta, UI_KEY_VALUE key_val)
{
    UINT8 Ret = 0;                        /* 0: call default signal func, 1: otherwise */
    UI_Normal_List_Only_Desc_t *Desc = (UI_Normal_List_Only_Desc_t *)g_Alarm_Mng.CurrDesc;

    if (KEY_RELEASED == key_sta)
    {
        switch (key_val)
        {
        case KEY_SOFTLEFT:  // OK
            if (0 == (g_Alarm_Mng.RepeatBitmapTemp & 0x7F))
            {
                g_Alarm_Mng.RepeatBitmapTemp = 0;
            }
            else
            {
                g_Alarm_Mng.RepeatBitmapTemp |= 0x80;
            }
            g_Alarm_Mng.Alarm.AlarmInfo[g_Alarm_Mng.CurrAlarmId].RepeatBitmap = g_Alarm_Mng.RepeatBitmapTemp;

            /* write to NVM */
            UI_NV_Write_Req(NV_SECTION_UI_ALARM, 0, sizeof(NV_UI_Alarm_t), (UINT8 *)&g_Alarm_Mng.Alarm);

            /* set recent alarm to rtc */
            Alarm_Set_Recent_Alarm(TRUE, FALSE);

            Display_Alarm_Status(ACT_ID_ALARM_REPEAT_SELECTED);
            Ret = 1;
            break;

        case KEY_SOFTRIGHT: // Back
            Nav_Back(ACT_ID_ANY);
            Ret = 1;
            break;

        case KEY_END:
            Nav_Home(ACT_ID_ANY);
            Ret = 1;
            break;

        case KEY_OK:    // Select
            break;

        case KEY_UP:
        case KEY_LEFT:
            if (Desc->List.Index > 0)
            {
                Desc->List.Index--;
            }
            else
            {
                Desc->List.Index = Desc->List.ListButtonCnt - 1;
            }
            break;

        case KEY_DOWN:
        case KEY_RIGHT:
            if (Desc->List.Index + 1 < Desc->List.ListButtonCnt)
            {
                Desc->List.Index++;
            }
            else
            {
                Desc->List.Index = 0;
            }
            break;

        default:
            break;
        } /* switch */
    }

    return(Ret);
} /* Alarm_Proc_Key_Alarm_Repeat_Select */

/**
 * process button Alarm Tone -> Standard
 */
VOID Alarm_Proc_Btn_Standard(lv_obj_t *Btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED != e) {
        return;
    }

    g_Alarm_Mng.Alarm.ToneIndex = 0;
    UI_NV_Write_Req(NV_SECTION_UI_ALARM, 0, sizeof(NV_UI_Alarm_t), (UINT8 *)&g_Alarm_Mng.Alarm);

    Display_Alarm_Status(ACT_ID_ALARM_TONE_SELECTED);
}

/**
 * process button Alarm Tone -> Ringing Tone
 */
VOID Alarm_Proc_Btn_Ringing_Tone(lv_obj_t *Btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED != e) {
        return;
    }

    Display_Alarm_Ringing_Tone();
}

/**
 * process key event of Alarm Tone
 */
UINT8 Alarm_Proc_Key_Alarm_Tone(lv_obj_t *obj, UI_KEY_STATUS key_sta, UI_KEY_VALUE key_val)
{
    UINT8 Ret = 0;                        /* 0: call default signal func, 1: otherwise */
    UI_Normal_List_Only_Desc_t *Desc = (UI_Normal_List_Only_Desc_t *)g_Alarm_Mng.CurrDesc;

    if (KEY_RELEASED == key_sta)
    {
        switch (key_val)
        {
        case KEY_SOFTRIGHT:
            Nav_Back(ACT_ID_ANY);
            Ret = 1;
            break;

        case KEY_END:
            Nav_Home(ACT_ID_ANY);
            Ret = 1;
            break;

        case KEY_OK:
            break;

        case KEY_UP:
        case KEY_LEFT:
            if (Desc->List.Index > 0)
            {
                Desc->List.Index--;
            }
            else
            {
                Desc->List.Index = Desc->List.ListButtonCnt - 1;
            }
            break;

        case KEY_DOWN:
        case KEY_RIGHT:
            if (Desc->List.Index + 1 < Desc->List.ListButtonCnt)
            {
                Desc->List.Index++;
            }
            else
            {
                Desc->List.Index = 0;
            }
            break;

        default:
            break;
        }
    }

    return(Ret);
}

/**
 * process button Ringing Tone -> OK
 */
VOID Alarm_Proc_Btn_Ringing_Tone_Select(lv_obj_t *Btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED != e) {
        return;
    }

    UI_Normal_List_Only_Desc_t *Desc    = (UI_Normal_List_Only_Desc_t *)g_Alarm_Mng.CurrDesc;
    UINT8                      BtnIndex = 0;

    BtnIndex                    = lv_list_get_btn_index(Desc->List.List, Btn);
    g_Alarm_Mng.Alarm.ToneIndex = BtnIndex + 1;

    UI_NV_Write_Req(NV_SECTION_UI_ALARM, 0, sizeof(NV_UI_Alarm_t), (UINT8 *)&g_Alarm_Mng.Alarm);

    Display_Alarm_Status(ACT_ID_ALARM_TONE_SELECTED);
}

/**
 * process key event of Alarm Ringing Tone
 */
UINT8 Alarm_Proc_Key_Alarm_Ringing_Tone(lv_obj_t *obj, UI_KEY_STATUS key_sta, UI_KEY_VALUE key_val)
{
    UINT8 Ret = 0;                        /* 0: call default signal func, 1: otherwise */
    UI_Normal_List_Only_Desc_t *Desc = (UI_Normal_List_Only_Desc_t *)g_Alarm_Mng.CurrDesc;
    
    if (KEY_RELEASED == key_sta)
    {
        switch (key_val)
        {
        case KEY_SOFTRIGHT:
            Nav_Back(ACT_ID_ANY);
            Ret = 1;
            break;

        case KEY_END:
            Nav_Home(ACT_ID_ANY);
            Ret = 1;
            break;

        case KEY_OK:
            break;

        case KEY_UP:
        case KEY_LEFT:
            if (Desc->List.Index > 0)
            {
                Desc->List.Index--;
            }
            else
            {
                Desc->List.Index = Desc->List.ListButtonCnt - 1;
            }
            break;

        case KEY_DOWN:
        case KEY_RIGHT:
            if (Desc->List.Index + 1 < Desc->List.ListButtonCnt)
            {
                Desc->List.Index++;
            }
            else
            {
                Desc->List.Index = 0;
            }

        default:
            break;
        }
    }

    return(Ret);
}

/**
 * process key event of Alarm Ring
 */
UINT8 Alarm_Proc_Key_Alarm_Ring(lv_obj_t *obj, UI_KEY_STATUS key_sta, UI_KEY_VALUE key_val)
{
    if ((KEY_RELEASED == key_sta)
        && ((KEY_SOFTRIGHT == key_val)
            || (KEY_OK == key_val)))
    {
        if (NULL == g_Alarm_Mng.Timer)
        {
            printf("%s: error, timer is NULL\n", __FUNCTION__);
        }
        else
        {
            Hal_Timer_Stop(g_Alarm_Mng.Timer);
            g_Alarm_Mng.Timer = NULL;
        }

        printf("%s: alarm id %d\n", __FUNCTION__, g_Alarm_Mng.RingAlarmId);
        if (NV_ALARM_MAX_ALARM_NUM <= g_Alarm_Mng.RingAlarmId)
        {
            printf("%s: wrong alarm id!\n", __FUNCTION__);
            return(1); /* 0: call default signal func, 1: otherwise */
        }

        if (KEY_SOFTRIGHT == key_val)
        {
            Alarm_Ring_Stop_Handle(TRUE);
        }
        else
        {
            Alarm_Ring_Stop_Handle(FALSE);
        }
    }

    return(1); /* 0: call default signal func, 1: otherwise */
}

/**
 * process status timeout
 * param (in) Para:  activity ID of status
 * return VOID
 */
VOID Alarm_Proc_Status_Timeout(VOID* Para)
{
    g_Alarm_Mng.StatusTimer = NULL;

    switch (g_Alarm_Mng.SrcActId)
    {
    case ACT_ID_ALARM_ON:
    case ACT_ID_ALARM_OFF:
    case ACT_ID_ALARM_ONCE_SELECTED:
    case ACT_ID_ALARM_REPEAT_SELECTED:
        Nav_Forward(g_Alarm_Mng.SrcActId, ACT_ID_ALARM_SET, NULL, NULL);
        break;

    case ACT_ID_ALARM_TONE_SELECTED:
        Nav_Forward(g_Alarm_Mng.SrcActId, ACT_ID_ALARM, NULL, NULL);
        break;

    case ACT_ID_ALARM_RING_OFF:
    case ACT_ID_ALARM_RING_DELAY:
        Nav_Back(g_Alarm_Mng.SrcActId);
        if ((IF_FRAMEWORK_INIT_CHARGE == Framework_Get_Init_Type())
            && (ACT_ID_CALENDAR_REMINDER_ALARM_RING != Nav_Get_Top()))
        {
            Framework_Poweroff_Stop_Alarm_Ind();
        }
        break;

    default:
        break;
    }
}
